package com.roomio.web;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

import com.roomio.core.model.RoomioException;
import com.roomio.core.security.model.User;

@Controller
public class BaseSpringController {
	
	
	protected User getCurrentUser(){
		User u = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(u==null){
			throw new RoomioException("No session");
		}
		return u;
	}
	
	
	protected String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
        String enc = httpServletRequest.getCharacterEncoding();
        if (enc == null) {
            enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
        }
        try {
            pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
        } catch (UnsupportedEncodingException uee) {}
        return pathSegment;
    }
}
